package com.cloud.tsp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.api.domain.model.ModelName;
import com.cloud.api.domain.model.NoticeModel;
import com.cloud.api.domain.model.OilInformationExcel;
import com.cloud.api.domain.model.OilModelInformation;
import com.cloud.api.utils.ExcelUtils;
import com.cloud.tsp.mapper.OilModelInformationMapper;
import com.cloud.tsp.service.IOilModelInformationService;
import com.cloud.tsp.service.IVehicleModelService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2023-12-25
 */
@Service("oilModelInformationService")
public class OilModelInformationServiceImpl extends ServiceImpl<OilModelInformationMapper, OilModelInformation> implements IOilModelInformationService {

    @Resource
    private OilModelInformationMapper oilModelInformationMapper;
    @Resource
    private IVehicleModelService vehicleModelService;
    @Resource
    private MessageSource messageSource;

    @Override
    public void exportOilInformation(String id, HttpServletResponse response) throws IOException {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.modelInformation";
        OilInformationExcel oilInformationExcel = new OilInformationExcel();
        OilModelInformation oilModelInformation = oilModelInformationMapper.selectById(id);
        ModelName modelName = vehicleModelService.selectParentNameById(id);
        if (oilModelInformation != null){
            BeanUtils.copyProperties(oilModelInformation,oilInformationExcel);
        }
        BeanUtils.copyProperties(modelName,oilInformationExcel);
        fileName = messageSource.getMessage(fileName, null, fileName, locale);
        List<OilInformationExcel> list = new ArrayList<>();
        list.add(oilInformationExcel);
        ExcelUtils.export(response,fileName,list,OilInformationExcel.class);
    }

    @Override
    public List<NoticeModel> selectNoticeModelByModel() {
        return oilModelInformationMapper.selectNoticeModelByModel();
    }
}